<?php
/**
 * DataStore
 * 
 * 作者:  姚刚 (yaogang@the9sense.com)
 * 创建时间: 2011-04-05 18:37:33
 * 修改记录: 
 * 
 * $Id: DataStore.php 18 2011-04-17 12:50:07Z coosuu $
 */

class DataStore {

    private $_dal;

    private $_config;

    private static $_loadedTables = array();

    /**
     * __call 
     * 
     * @param  mixed $method 
     * @param  mixed $params 
     * @return void
     */
    public function __call($method, $params) {

        return call_user_func_array(array($this->getDalObj(), $method), $params);
    }

    /**
     * __construct 
     * 
     * @param  mixed $table 
     * @return void
     */
    public function __construct($table) {

        $this->_config = Config::get('DB.' . $table);
        if (!is_array($this->_config) || !$this->_config['type']) {
            throw new DataStore_Exception($table . ' Config Error: $config["type"] could not be empty/null');
        }

        $this->_dal = DataStore::factory($table, $this->_config);
    }

    /**
     * getDalObj 
     * 
     * @return void
     */
    public function getDalObj() {

        return $this->_dal;
    }

    /**
     * &factory 
     * 
     * @param  mixed $table 
     * @param  mixed $config 
     * @return void
     */
    public static function &factory($table, $config) {

        if (!in_array($config['type'], array('mysql'))) {
            throw new DataStore_Exception('DataStore donnot support driver:' . $config['type']);
        }

        if (!array_key_exists($table, self::$_loadedTables)) {
            switch ($config['type']) {
                case 'mysql' :
                default :
                    self::$_loadedTables[$table] = new DataStore_Mysql($table, $config);
            }
        }

        return self::$_loadedTables[$table];
    }
}
